$(function () {
    let $cont = $(".test-content");
    let $searchbtn = $("#btn-search");
    let $downloadbtn = $(".test-download");
    let $downloadbtn1 = $(".test1-download");
    let iPage = 1;
    let bIsLoadData = true;// 是否正在向后台加载数据



    $searchbtn.click(function () {
        let sCont = $cont.val();
        if (!sCont){
            message.showError("查询值不能为空")
            return
        }

        let SdataParams = {
            "content": sCont,
        }

        $.ajax({
            url: '/searches/',
            type: 'POST',
            data: JSON.stringify(SdataParams),
            dataType: 'json',
        })
            .done(function (res) {
                if (res.errno === "0") {
                    if (iPage === 1) {
                        $("#tbody").html("")
                    }
                    res.data.pro_inf.forEach(function (one_contents) {
                        let content = `<tr data-id="${one_contents.id}" class="property_detail" id="light">
                                  <td>${one_contents.number}</td>
                                  <td>${one_contents.idcn__name}</td>
                                  <td>${one_contents.idcr__rname}</td>
                                  <td>${one_contents.pn}</td>
                                  <td>${one_contents.sn}</td>
                                  <td>${one_contents.mt}</td>
                                  <td>${one_contents.cb}</td>
                                  <td>${one_contents.ul}</td>
                                  <td>${one_contents.nip}</td>
                                  <td>${one_contents.nmask}</td>
                                  <td>${one_contents.ngateway}</td>
                                  <td>${one_contents.wip}</td>
                                  <td>${one_contents.wmask}</td>
                                  <td>${one_contents.wgateway}</td>
                                  <td>${one_contents.mip}</td>
                                  <td>${one_contents.mmask}</td>
                                  <td>${one_contents.mgateway}</td>
                                  <td>${one_contents.ntvlan}</td>
                                  <td>${one_contents.navlan}</td>
                                  <td>${one_contents.imode}</td>
                                  <td>${one_contents.env}</td>
                                  <td>${one_contents.equ_ty}</td>
                                  <td>${one_contents.arrtime}</td>
                                  <td>${one_contents.transfer}</td>
                                  <td>${one_contents.pupdate}</td>
                                  <td>${one_contents.obip}</td>
                                  <td>${one_contents.move}</td>
                                  <td>${one_contents.offline}</td>
                                  <td>${one_contents.Hwc}</td>
                                  <td>${one_contents.re_install}</td>
                                  <td>${one_contents.remarks}</td>
                                  <td>${one_contents.supplier}</td>
                          </tr>`;
                        $(".search-box").append(content);
                    })
                    // 数据加载完毕，设置正在加载数据的变量为false，表示当前没有在加载数据
                    bIsLoadData = false;
                    let $detailbtn = $(".property_detail");

                    $detailbtn.click(function () {
                        let sPropertyId = $(this).data('id');
                        $.ajax({
                            // 请求地址
                            url: "/pdetails/" + sPropertyId + "/",  // url尾部需要添加/
                            // 请求方式
                            type: "get",
                            dataType: "json",
                        })
                            .done(function (res) {
                                if (res.errno === "0") {
                                    if (iPage === 1) {
                                        $("#kkk").html("")
                                    }
                                    res.data.per_pro.forEach(function (one_contents) {
                                        let content = `<tr style="padding-bottom: 10px">
                                                  <td >机房名称:<input type="text" id="idcname" name="idcn.name" value="${one_contents.idcn__name}"></td>
                                                  <td>房间编号:<input type="text" id="idrname" name="idcr.rname" value="${ one_contents.idcr__rname}"</td>
                                              </tr>
                                              <tr>
                                                <td>资产编号:<input type="text" id="pn" name="pn" value="${ one_contents.pn}"></td>
                                                <td>sn:<input type="text" id="sn" name="sn" value="${ one_contents.sn }"></td>
                                               </tr>
  <tr>
    <td>机列:<input type="text" id="mt" name="mt" value="${ one_contents.mt }"></td>
    <td>机柜:<input type="text" id="cb" name="cb" value="${ one_contents.cb }"></td>
  </tr>
  <tr>
    <td>U位:<input type="text" id="ul" name="ul" value="${ one_contents.ul }"></td>
    <td>内网ip:<input type="text" id="nip" name="nip" value="${ one_contents.nip }"></td>
  </tr>
  <tr>
    <td>内网掩码:<input type="text" id="nmask" name="nmask" value="${ one_contents.nmask }"></td>
    <td>内网网关:<input type="text" id="ngateway" name="ngateway" value="${ one_contents.ngateway }"></td>
  </tr>
   <tr>
    <td>外网ip:<input type="text" id="wip" name="wip" value="${ one_contents.wip }"></td>
    <td>外网掩码:<input type="text" id="wmask" name="wmask" value="${ one_contents.wmask }"></td>
  </tr>
     <tr>
    <td>外网网关:<input type="text" id="wgateway" name="wgateway" value="${ one_contents.wgateway }"></td>
    <td>带外ip:<input type="text" id="mip" name="mip" value="${ one_contents.mip }"></td>
  </tr>
       <tr>
    <td>带外掩码:<input type="text" id="mmask" name="mmask" value="${ one_contents.mmask }"></td>
    <td>带外网关:<input type="text" id="mgateway" name="mgateway" value="${ one_contents.mgateway }"></td>
  </tr>
     <tr>
    <td>内网trunk vlan:<input type="text" id="ntvlan" name="ntvlan" value="${ one_contents.ntvlan }"></td>
    <td>内网all vlan:<input type="text" id="navlan" name="navlan" value="${ one_contents.navlan }"></td>
  </tr>
      <tr>
    <td>安装模板:<input type="text" id="imode" name="imode" value="${ one_contents.imode }"></td>
    <td>环境:<input type="text" id="env" name="env" value="${ one_contents.env }"></td>
  </tr>
     <tr>
    <td>设备型号:<input type="text" id="equ_ty" name="equ_ty" value="${ one_contents.equ_ty }"></td>
    <td>到货时间:<input type="text" id="arrtime" name="arrtime" value="${ one_contents.arrtime }"></td>
  </tr>
    <tr>
    <td>是否移交:<input type="text" id="transfer" name="transfer" value="${ one_contents.transfer }"></td>
    <td>资产是否更新:<input type="text" id="pupdate" name="pupdate" value="${ one_contents.pupdate }"></td>
  </tr>
      <tr>
    <td>带外ip是否开通:<input type="text" id="obip" name="obip" value="${ one_contents.obip }"></td>
    <td>搬迁:<input type="text" id="move" name="move" value="${ one_contents.move }"></td>
  </tr>
   <tr>
    <td>下线:<input type="text" id="offline" name="offline" value="${ one_contents.offline }"></td>
    <td>硬件变更:<input type="text" id="Hwc" name="Hwc" value="${ one_contents.Hwc }"></td>
  </tr>
<tr>
      <td>重装:<input type="text" id="re_install" name="re_install" value="${ one_contents.re_install }"></td>
      <td>备注:<input type="text" id="remarks" name="remarks" value="${ one_contents.remarks }"></td>
    </tr>
<tr>
      <td>型号:<input type="text" id="supplier" name="supplier" value="${ one_contents.supplier }"></td>
    </tr>`;
                                        $(".outlook").append(content);
                                        let light = document.getElementById("light");
                                        light.style.display = 'block';
                                        $("#fade").css('display', 'block');
                                    })
                                    let $btn_update = $(".per_up")
                                    $btn_update.click(function () {
                                        let sIdcname = $("#idcname").val();
                                        let sIdrname = $("#idrname").val();
                                        let sPn = $("#pn").val();
                                        if (!sPn) {
                                            message.showError('资产编号不能为空！');
                                            return
                                        }
                                        let sSn = $("#sn").val();
                                        let sMt = $("#mt").val();
                                        let sCb = $("#cb").val();
                                        let sUl = $("#ul").val();
                                        let sNip = $("#nip").val();
                                        let sNmask = $("#nmask").val();
                                        let sNgateway = $("#ngateway").val();
                                        let sWip = $("#wip").val();
                                        let sWmask = $("#wmask").val();
                                        let sWgateway = $("#wgateway").val();
                                        let sMip = $("#mip").val();
                                        let sMmask = $("#mmask").val();
                                        let sMgateway = $("#mgateway").val();
                                        let sNtvlan = $("#ntvlan").val();
                                        let sNavlan = $("#navlan").val();
                                        let sImode = $("#imode").val();
                                        let sEnv = $("#env").val();
                                        let sEqu_ty = $("#equ_ty").val();
                                        let sArrtime = $("#arrtime").val();
                                        let sTransfer = $("#transfer").val();
                                        let sPupdate = $("#pupdate").val();
                                        let sObip = $("#obip").val();
                                        let sMove = $("#move").val();
                                        let sOffline = $("#offline").val();
                                        let sHwc = $("#Hwc").val();
                                        let sRe_install = $("#re_install").val();
                                        let sRemarks = $("#remarks").val();
                                        let sSupplier = $("#supplier").val();

                                        let data = {
                                            "idc_name": sIdcname,
                                            "idr_name": sIdrname,
                                            "pn": sPn,
                                            "sn": sSn,
                                            "mt": sMt,
                                            "cb": sCb,
                                            "ul": sUl,
                                            "nip": sNip,
                                            "nmask": sNmask,
                                            "ngateway": sNgateway,
                                            "wip": sWip,
                                            "wmask": sWmask,
                                            "wgateway": sWgateway,
                                            "mip": sMip,
                                            "mmask": sMmask,
                                            "mgateway": sMgateway,
                                            "ntvlan": sNtvlan,
                                            "navlan": sNavlan,
                                            "imode": sImode,
                                            "env": sEnv,
                                            "equ_ty": sEqu_ty,
                                            "arrtime": sArrtime,
                                            "transfer": sTransfer,
                                            "pupdate": sPupdate,
                                            "obip": sObip,
                                            "move": sMove,
                                            "offline": sOffline,
                                            "Hwc": sHwc,
                                            "re_install": sRe_install,
                                            "remarks": sRemarks,
                                            "supplier": sSupplier,
                                        };
                                        $.ajax({
                                            // 请求地址
                                            url: "/searches/",  // url尾部需要添加/
                                            // 请求方式
                                            type: "PUT",
                                            dataType: "json",
                                            data: JSON.stringify(data),
                                        })
                                            .done(function (res) {
                                                if (res.errno === "0") {
                                                    let light = document.getElementById("light");
                                                    light.style.display = 'none';
                                                    $("#fade").css('display', 'none');
                                                    message.showSuccess("更新成功");
                                                    $searchbtn.click();
                                                } else {
                                                    message.showError(res.errmsg);
                                                }
                                            })
                                            .fail(function () {
                                                message.showError("服务器超时,请重试")
                                            })

                                    })

                                } else {
                                    message.showError(res.errmsg);
                                }
                            })
                            .fail(function () {
                                message.showError("服务器超时,请重试")
                            })
                    })

                } else {
                    message.showError(res.errmsg);
                }

            })
            .fail(function () {
                message.showError("服务器超时,请重试")
            })

    });

    //  $("#fileId").ajaxForm({
    //     //定义返回JSON数据，还包括xml和script格式
    //     dataType:'json',
    //     // beforeSend: function() {
    //     //     //表单提交前做表单验证
    //     // },
    //     success: function(data) {
    //         //提交成功后调用
    //         window.open("http://www.shenpeng.online:18002/pdetails/")
    //     }
    // });

    $downloadbtn.click(function () {
        //     let sCont = $cont.val();
        //
        //     let SdataParams = {
        //        "content":sCont,
        //    }
        //    $.ajax({
        //        url:'/tests/download/',
        //        type:'POST',
        //        data:JSON.stringify(SdataParams),
        //        // 请求内容的数据类型（前端发给后端的格式）
        //        // contentType: "application/json; charset=utf-8",
        //        dataType:'json',
        //    })
        //     .done(function (response) {
        //         message.showInfo(response.responseText);
        //     })
        //    .fail(function () {
        //        message.showError("服务器超时,请重试")
        //    })
        // })
        // window.location.href = "/updates";
        let sCont = $cont.val();

        if(!(sCont)){
            return message.showError("无数据可下载")
        }

        var json  = {
            "content":sCont
        };
        var form = $("<form>");
        form.attr('style', 'display:none');
        form.attr('target', '');
        form.attr('method', 'post'); //请求方式
        form.attr('action', '/download/');//请求地址

        var input1 = $('<input>');//将你请求的数据模仿成一个input表单
        input1.attr('type', 'hidden');
        input1.attr('name', 'json');//该输入框的name
        input1.attr('value',JSON.stringify(json));//该输入框的值

        $('body').append(form);
        form.append(input1);

        form.submit();
        form.remove();
    });

    $downloadbtn1.click(function () {
        //     let sCont = $cont.val();
        //
        //     let SdataParams = {
        //        "content":sCont,
        //    }
        //    $.ajax({
        //        url:'/tests/download/',
        //        type:'POST',
        //        data:JSON.stringify(SdataParams),
        //        // 请求内容的数据类型（前端发给后端的格式）
        //        // contentType: "application/json; charset=utf-8",
        //        dataType:'json',
        //    })
        //     .done(function (response) {
        //         message.showInfo(response.responseText);
        //     })
        //    .fail(function () {
        //        message.showError("服务器超时,请重试")
        //    })
        // })
        // window.location.href = "/updates";
        let sCont = $cont.val();

        if(!(sCont)){
            return message.showError("无数据可下载")
        }

        var json  = {
            "content":sCont
        };
        var form = $("<form>");
        form.attr('style', 'display:none');
        form.attr('target', '');
        form.attr('method', 'post'); //请求方式
        form.attr('action', '/download1/');//请求地址

        var input1 = $('<input>');//将你请求的数据模仿成一个input表单
        input1.attr('type', 'hidden');
        input1.attr('name', 'json');//该输入框的name
        input1.attr('value',JSON.stringify(json));//该输入框的值

        $('body').append(form);
        form.append(input1);

        form.submit();
        form.remove();
    });

// // get cookie using jQuery
//   function getCookie(name) {
//     let cookieValue = null;
//     if (document.cookie && document.cookie !== '') {
//       let cookies = document.cookie.split(';');
//       for (let i = 0; i < cookies.length; i++) {
//         let cookie = jQuery.trim(cookies[i]);
//         // Does this cookie string begin with the name we want?
//         if (cookie.substring(0, name.length + 1) === (name + '=')) {
//           cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
//           break;
//         }
//       }
//     }
//     return cookieValue;
//   }
//
//   function csrfSafeMethod(method) {
//     // these HTTP methods do not require CSRF protection
//     return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
//   }
//
//   // Setting the token on the AJAX request
//   $.ajaxSetup({
//     beforeSend: function (xhr, settings) {
//       if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
//         xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
//       }
//     }
//   });

//   function post(path, shipmentMap, method) {
//     method = method || "post"; // Set method to post by default if not specified.
//     var token = $(‘meta[name="_csrf"]‘).attr(‘content‘);
//     var tokenName = $(‘meta[name="_csrf_header"]‘).attr(‘content‘);
//     console.log(token);
//     // The rest of this code assumes you are not using a library.
//     // It can be made less wordy if you use one.
//     var form = document.createElement("form");
//     form.setAttribute("method", method);
//     form.setAttribute("action", path);
//
//
//     var hiddenField = document.createElement("input");
//     hiddenField.setAttribute("type", "hidden");
//     hiddenField.setAttribute("name", "requestMap");
//     hiddenField.setAttribute("value", JSON.stringify(shipmentMap));
//
//     form.appendChild(hiddenField);
//
//     var csrfField = document.createElement("input");
//     csrfField.setAttribute("type", "hidden");
//     csrfField.setAttribute("name", "_csrf");
//     csrfField.setAttribute("value", token);
//
//     form.appendChild(csrfField);
//
//
//     document.body.appendChild(form);
//     form.submit();
// }

});